package com.supercube.wechat.controller;

import com.supercube.common.utils.DateUtils;
import com.supercube.common.utils.QiNiuUtils;
import com.supercube.common.utils.R;
import com.supercube.common.utils.StringUtils;
import com.supercube.sport.yoga.domain.MemberBodyInfoDTO;
import com.supercube.sport.yoga.domain.VenueInfoDTO;
import com.supercube.sport.yoga.service.MemberBodyInfoService;
import com.supercube.sport.yoga.service.MemberService;
import com.supercube.sport.yoga.service.VenueInfoService;
import com.supercube.sport.yoga.vo.MemberVO;
import com.supercube.system.domain.UserDO;
import com.supercube.system.service.UserService;
import com.supercube.wechat.service.MiniAppApiService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 小程序员工Api控制器<br />
 * 小程序员工的所有Api写在此处
 *
 * @author djzhao
 * @date 19/11/01 22:13
 * @email djzhao627@gmail.com
 */
@RestController
@RequestMapping("/miniapp/employee")
public class MiniAppEmployeeApiController {

    @Autowired
    private MiniAppApiService miniAppApiService;

    @Autowired
    private VenueInfoService venueInfoService;
    @Autowired
    private UserService userService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private MemberBodyInfoService memberBodyInfoService;

    private final org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass());

    @GetMapping("/uptoken")
    public R getUploadToken() {
        String uploadToken = QiNiuUtils.getUploadToken();
        Map<String, Object> result = new HashMap<>();
        result.put("uptoken", uploadToken);
        return R.ok(result);
    }

    /**
     * 获取当前员工所管理场馆的会员<br />
     * 员工ID（sys_user）->可操作场馆表（T_correlation_venue）->场馆信息（t_venue_info）->会员卡（T_card_support_venues）->会员卡（t_member_card_basic_info）->会员绑卡（t_member_card_binding_info）->会员（t_member）
     *
     * @return 会员列表
     */
    @PostMapping("/getMyVenueUsers")
    public R getMyVenueUsers(String employeeId, String venueId) {
        UserDO userDO = userService.get(Long.parseLong(employeeId));
        if (userDO == null) {
            return R.error(1, "员工信息不正确");
        }
        // 员工关联的场馆列表
        List<VenueInfoDTO> venueInfoDTOS = venueInfoService.queryVenueInfoListByUserId(employeeId);
        if (StringUtils.isEmpty(venueId)) {
            venueId = venueInfoDTOS.get(0).getId();
        }
        // 场馆关联的会员列表
        List<MemberVO> memberVOS = memberService.getMemberByVenueId(venueId);
        Map<String, Object> result = new HashMap<>();
        result.put("venues", venueInfoDTOS);
        result.put("members", memberVOS);
        return R.ok(result);
    }

    /**
     * 体测图片信息上传
     *
     * @param memberBodyInfoDTO
     * @return
     */
    @PostMapping("/uploadBodyTest")
    public R submitMemberBodyTestImage(MemberBodyInfoDTO memberBodyInfoDTO) {
        int save;
        try {
            memberBodyInfoDTO.setCreateTime(DateUtils.getDate("yyyy-MM-dd HH:mm:ss"));
            memberBodyInfoDTO.setId(UUID.randomUUID().toString());
            save = memberBodyInfoService.save(memberBodyInfoDTO);
        } catch (Exception e) {
            logger.info(e.getMessage());
            return R.error(1, e.getMessage());
        }
        if (save <= 0) {
            return R.error(1, "上传体测信息失败");
        }
        return R.ok();
    }

}
